package com.shigu.admin.manager.actions;

import com.alibaba.fastjson.JSON;
import com.opentae.data.mall.beans.ManagerPermission;
import com.shigu.admin.manager.bo.AddpermissionBO;
import com.shigu.admin.manager.bo.PageSearchBO;
import com.shigu.admin.manager.common.CustomPager;
import com.shigu.admin.manager.common.JSONResult;
import com.shigu.admin.manager.services.AdminPermissionService;
import com.shigu.admin.manager.services.AdminRoleService;
import com.shigu.admin.manager.vo.ZTreeVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 权限管理
 * Created by Licca on 16/5/13.
 */
@Controller
public class AdminPermissionAction extends BaseAction {

    @Autowired
    AdminPermissionService adminPermissionService;

    @Autowired
    AdminRoleService adminRoleService;


    /**
     * 对子节点操作
     * @return
     */
    @RequestMapping("subpermission")
    @ResponseBody
    public JSONResult subpermission(Integer submit, Long pid, String tag, String tagComment){
        if(pid==null||submit==null){
            obj= JSONResult.error();
            return obj;
        }
        if(submit==1){//添加子节点
            Long subPid = adminPermissionService.addPermissionCheckTag(tag, tagComment, pid);
            adminRoleService.changeRelation(1L, subPid);
            obj= JSONResult.success();
        }else if(submit==2){//修改当前节点
            adminPermissionService.modifyPermission(tag,tagComment,pid);
            obj= JSONResult.success();
        }
        return obj;
    }
    /**
     * 权限列表
     * @return
     */
    @RequestMapping("permissionlist")
    @RequiresPermissions("permission:list")
    public String list(PageSearchBO bo, Model model){
        if(bo.getPageNo()==null){
            bo.setPageNo(1);
        }
        model.addAttribute("keyword",bo.getKeyword());
        model.addAttribute("pageNo",bo.getPageNo());
        CustomPager<ManagerPermission> pager=adminPermissionService.selList(bo.getKeyword(), bo.getPageNo(), bo.pageSize);
        model.addAttribute("pager",pager);
        return "sys/permission";
    }

    /**
     * 添加权限
     * @return
     */
    @RequestMapping("addpermission")
    @RequiresPermissions("permission:top")
    public String add(AddpermissionBO bo, Model model){
        String targetView="sys/addpermission";
        if(bo.getSubmit()==null){
            return targetView;
        }
        if((bo.getTag()==null||"".equals(bo.getTag()))&&bo.getSubmit()!=null&&bo.getSubmit()==1){
            String result="tag";
            model.addAttribute("result",result);
            return targetView;
        }
        if((bo.getTagComment()==null||"".equals(bo.getTagComment()))&&bo.getSubmit()!=null&&bo.getSubmit()==1){
            String result="tagComment";
            model.addAttribute("result", result);
            return targetView;
        }
        String msg=null;
        ManagerPermission mp=adminPermissionService.checkTag(bo.getTag());
        if(mp==null){//可以用
            Long pid=adminPermissionService.addPermissionCheckTag(bo.getTag(),bo.getTagComment(),null);
            adminRoleService.changeRelation(1L, pid);// 每添加一个新的权限，自动赋予ID=1用户，这通常是顶级管理员
            msg="添加成功,pid:"+pid;
            bo.setTagComment(null);
            bo.setTag(null);
            model.addAttribute("msg",msg);
            return targetView;
        }else{
                    String result="tag";
                    msg="权限标签已经被使用";
                    model.addAttribute("result",result);
                    model.addAttribute("msg",msg);
                    return targetView;
        }
    }


    @RequestMapping("addsubpermission")
    @RequiresPermissions("permission:sub")
    public String addsub3(AddpermissionBO bo, Model model){
        String treeJsonstr= JSON.toJSONString(adminPermissionService.selAllPermissionFortreeByParentId(null));
        model.addAttribute("treeJsonstr",treeJsonstr);
        add(bo,model);
        return "sys/addpermissionsub";
    }

    /**
     * 返回子节点的子节点
     * @param pid
     * @return
     */
    @RequestMapping("subPermission")
    @ResponseBody
    public List<ZTreeVO> synSubPermission(Long pid){
        return adminPermissionService.selAllPermissionFortreeByParentId(pid);
    }
    /**
     * 修改权限状态
     * @return
     */
    @RequestMapping("cgpermission")
    @ResponseBody
    public JSONResult cgpermission(Long pid){
        if(pid==null){
            obj= JSONResult.error();
            return obj;
        }
        int result=adminPermissionService.changeStatus(pid);
        if(result==0) {
            obj= JSONResult.error("0");
        }else{
            obj= JSONResult.success();
        }
        return obj;
    }
    @Override
    public Integer getLeftflag() {
        return leftflag;
    }

    @Override
    public void setLeftflag(Integer leftflag) {
        this.leftflag = leftflag;
    }
}
